﻿@model ByHand.Model.Order
@{
    List<string> key = new List<string>();
    foreach (var item in Model.OrderDetail)
    {
        if (item.OptionsValue != "undefined")
        {
            var temp = item.OptionsValue.Split('-').Where(s => s.Length > 0).ToList();
            foreach (var i in temp)
            {
                var t = i.Split('_')[0];
                if (!key.Contains(t))
                {
                    key.Add(t);
                }
            }
        }
    }
    var valuesAsList = Enum.GetValues(typeof(ByHand.Services.Code.EOrderStatus)).Cast<ByHand.Services.Code.EOrderStatus>().ToList();
}
@using (Html.BeginForm("Update", "Order", FormMethod.Post, new { Class = "form-horizontal", id ="form-update-order" }))
{
    <input type="hidden" value="@Model.ID" name="oID" />
    <h3>Thông tin đơn hàng 
    <select style="float: right" name="status">
        @foreach (var item in valuesAsList)
        {
            <option @(Model.Status == (int)item ? "selected":"") value="@((int)item)">@item.ToString().Replace("Order_","").Replace("_"," ")</option>
        }
    </select>
</h3>
    <div class="input-prepend div-space">
        <span class="add-on">Tên hàng</span>
        <input class="span2" disabled type="text" value="@Model.Product.Title" />
    </div>
    <div class="input-prepend div-space">
        <span class="add-on">Người nhận</span>
        <input class="span2" name="toName" type="text" value="@Model.ToName" />
    </div>
    <div class="input-prepend div-space" style="margin-bottom: 5px; width: 470px">
        <span class="add-on">Phone</span>
        <input class="span2" name="toPhone" type="text" value="@Model.ToPhone" >
    </div>
    <div class="input-prepend div-space" style="margin-bottom: 5px; width: 470px">
        <span class="add-on">Địa Chỉ</span>
        <input class="span2" type="text" name="toAddress" value="@Model.ToAddress">
    </div>
    <div class="input-prepend div-space" style="margin-bottom: 5px; width: 470px">
        <span class="add-on">Tổng giá</span>
        <input class="span2" disabled  type="text" value="@(Model.Price * Model.Quantity)" placeholder="Username">
    </div>
    <div class="input-prepend div-space" style="margin-bottom: 5px; width: 470px">
        <span class="add-on">Địa chỉ mua</span>
        <input class="span2" disabled  type="text" value="@(Model.RefLocation)" placeholder="Username">
    </div>
    <table class="table table-hover table-bordered">
        <thead>
            <tr>
                <th>Mã sản hàng</th>
                @foreach (var item in key)
                {
                    <th>
                        @item
                    </th>
                }
                <th>Số lượng</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.OrderDetail)
            {
                <tr>
                    <td>
                        @Model.Product.ProductCode
                    </td>
                    @foreach (var i in key)
                    {
                        <td>
                            @(item.OptionsValue.Split('-').Where(s => s.Contains(i)).FirstOrDefault().Split('_')[1])
                        </td>
                    }
                    <td>
                        @(item.Quantity)
                    </td>
                </tr>
            }
        </tbody>
    </table>
    <p style="text-align: center">
        <button class="btn btn-primary" type="submit">Update</button>
    </p>
}
<script type="text/ecmascript">
    $('#form-update-order').ajaxForm(function (data) {
        if (data) {
            window.location = window.location;
        }
    });
</script>